package com.esperia09.webservice.mixibot.voice;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;

import com.esperia09.webservice.mixibot.common.SendRequest;

/**
 * mixi voiceへつぶやくためのクラス
 * @author esperia
 *
 */
public class SendVoiceRequest extends SendRequest {
	private static final String TWEET_URL = "http://api.mixi-platform.com/2/voice/statuses/update"; 

	/**
	 * Refresh Tokenを使ってAccessTokenを取得する
	 * @param authCode
	 * @return
	 * @throws IOException
	 */
	public static String sendVoice(String accessToken, String voice) {
		String result = "";
		HttpURLConnection con = null;
		try {
		    URL url = new URL(TWEET_URL);
		    con = (HttpURLConnection) url.openConnection();
		    con.setRequestProperty("Content-type", "application/x-www-form-urlencoded");
		    con.setRequestProperty("Authorization", "OAuth " + accessToken);
	
		    String postString = "status=" + URLEncoder.encode(voice, "UTF-8");
		    result = sendPost(con, postString);
		} catch(UnsupportedEncodingException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
		    if (con != null) con.disconnect();
		}
		
		return result;
	}
}
